package org.rcy.ruicingmarket.controller;


import org.rcy.ruicingmarket.cart.CartDelVo;
import org.rcy.ruicingmarket.cart.CartEditVo;
import org.rcy.ruicingmarket.cart.CartService;
import org.rcy.ruicingmarket.cart.CartAddVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author RuicyWu
 * @Description
 * @date 2022/1/5 14:18
 */

@RestController
@RequestMapping("/car")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加到购物车
     * @param vo
     * @return
     */
    @PostMapping("/addtocar")
    public ResultMessage addToCar(HttpServletRequest request , @RequestBody CartAddVo vo)
    {
        String uid = (String)request.getHeader("uid");
        if(StringUtils.isBlank(uid)){
            return ResultMessage.error();
        }
        vo.setUid(uid);
        boolean hasCart = cartService.findByUidAndBid(vo.getUid(), vo.getSkuId());
        try {
            if(!hasCart)
            {
                cartService.add(vo);
            }
            else {
                CartEditVo cartEditVo = new CartEditVo();
                cartEditVo.setCount(vo.getCount());
                cartEditVo.setSkuId(vo.getSkuId());
                cartEditVo.setUid(vo.getUid());
                cartService.editCount(cartEditVo);
            }
            return ResultMessage.success();
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultMessage.error();
        }

    }

    /**
     * 从购物车中移除商品
     * @param vo
     * @return
     */
    @DeleteMapping("/remove")
    public ResultMessage removeBookFromCar(@RequestBody CartDelVo vo)
    {
        try {
            String uid = (String)vo.getUid();
            if(StringUtils.isBlank(uid)){
                return ResultMessage.error();
            }
            cartService.remove(vo);
            return ResultMessage.success();
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultMessage.error();
        }
    }


    /**
     * 修改购物车
     * @param vo
     * @return
     */
    @PutMapping("/editcart")
    public ResultMessage editCarCount(@RequestBody CartEditVo vo){
          try {
              String uid = vo.getUid();
              if(StringUtils.isBlank(uid)){
                  return ResultMessage.error();
              }
              cartService.editCount(vo);
                return ResultMessage.success();
          }catch (Exception e){
              e.printStackTrace();
              return ResultMessage.error();
          }
    }


    /**
     * 获取购物车列表
     * @return
     */
    @GetMapping("/carlist")
    public ResultMessage getCarList(HttpServletRequest request)
    {
        try {
            String uid = (String)request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                return ResultMessage.error();
            }
            return ResultMessage.success(cartService.getCarListByUid(Long.parseLong(uid)));
        }catch (Exception e){
            e.printStackTrace();
            return ResultMessage.error();
        }

    }

}
